// pages/integral_growth/integral_growth.js
var that,
  api = new(require('../../utils/api.js')),
  staticimg=api.getstaticimg(),
  headimg = api.getimgurl(),
  // md5 = require('../../utils/md5.js'),
  app = getApp(),
  util = new(require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pagesize:20,
    start:1, 
    hiddenmodal:!0,
    // all:'',
    date:'',
    fromwhere:'',
    score:'',
    list:[],
    inputmoney:0,
    // judge:'',
    staticimg,
    storage:0,
    iswithdrawaling:!1,
    selectindex:0,
    state:'ALL'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this
    let user=wx.getStorageSync('user')
     that.setData({usertype:user.type})
    //that.setData({usertype:"CONSUMER"})
    that.getdata()
  },
  inputmoney(e){
    this.setData({inputmoney:e.detail.value})
//console.log(e.detail.value,'inputmoney');
  },
  getdata(){
    console.log(1111111);
    
    wx.showLoading({
      title: '加载中',
    })
    let { pagesize,start,state}= that.data,oldlist = that.data.list;
    // if(judge==2){
      api.getstoragelist({pagesize,start,state},res => {
        console.log(33333);
        
        wx.hideLoading({
          complete: (res) => {},
        })
      if (res.data.issuccess == 1) {
        // that.formatData(res.data.list)
        if (that.data.usertype =='CONSUMER'){
          that.setData({ withdrawal: (res.data.storage/100).toFixed(2)})
        }
        let {list,iswithdrawal,Withdrawalprice}=res.data, output = 0;
        console.log(list,'list');
        // if(that.data.usertype=='RECYCLER'||that.data.usertype=='AGENT'){
        //   let newlist = [];
        //   list.forEach(x=>{if(x.item.storage>=0){ newlist.push(x)}})
        //   list = newlist;
        // }
        list.forEach(x=>{
          x.updateDate = util.formatTimenosecend(new Date(x.date*1000))
         // output +=x.item.storage
          
          x['storage']=(x.storage/100).toFixed(2);
          if(x.source=='RECYCLER') x['storage'] = ((-x.storage/100).toFixed(2)); 
          if(x.paytype=='OFF') x['storage']= Math.abs((x.storage/100)).toFixed(2)
          x['sourcechinese'] = x.source == "BK_RECHARGE" ? '后台储值' :x.source=="RECYCLE"?'回收物品':x.source=="AGENT"?"代理商":x.source=="RECHARGE"?"充值":x.source=="RECYCLER"?"回收":x.source=="WITHDRAWAL"?"提现":"超管"
        })
        console.log(list.length,'list.length');
        that.setData({ output:(res.data.output/ 100).toFixed(2) //(res.data.output/ 100).toFixed(2)
        
        })

        // if(that.data.usertype=="AGENT"){
        //   that.setData({ output: (res.data.output/ 100).toFixed(2)})

        // }
        list = oldlist.concat(list);

        let storage=(that.data.usertype=='AGENT'||that.data.usertype=='OAAGENT')?(res.data.storage/ 100).toFixed(2):(res.data.output/ 100).toFixed(2),totalprice=parseFloat((Withdrawalprice/100).toFixed(2))
        let a = res.data.withdrawal/100 - totalprice;
        console.log(a,'aaaaaaaaa');
        
        that.setData({list,storage,iswithdrawal, reachbtm: list.length == res.data.total,totalprice,canwithdrawal: !iswithdrawal&&(res.data.storage/100 - totalprice>=0)})
        //that.setData({list,storage,iswithdrawal, reachbtm: list.length == res.data.total,totalprice,canwithdrawal:!0 })
      }
    });
   
  },
  toselect(e){
    console.log(2222);
    
    let {state,index} =e.currentTarget.dataset
    that.setData({state,selectindex:index,list:[]},()=>{
      that.getdata()
    })
    // wx.showLoading({
    //   title: '加载中',
    // })
    // let { pagesize,start }= that.data,oldlist = that.data.list;
    // // if(judge==2){
    //   api.getstoragelist({pagesize,start,state},res => {
    //     wx.hideLoading({
    //       complete: (res) => {},
    //     })
    //   if (res.data.issuccess == 1) {
    //     // that.formatData(res.data.list)
    //     if (that.data.usertype =='CONSUMER'){
    //       that.setData({ withdrawal: (res.data.withdrawal/100).toFixed(2)})
    //     }
    //     let {list,iswithdrawal,Withdrawalprice}=res.data, output = 0;
    //     console.log(list,'list');
    //     // if(that.data.usertype=='RECYCLER'||that.data.usertype=='AGENT'){
    //     //   let newlist = [];
    //     //   list.forEach(x=>{if(x.item.storage>=0){ newlist.push(x)}})
    //     //   list = newlist;
    //     // }
    //     list.forEach(x=>{
    //       x.updateDate = util.formatTimenosecend(new Date(x.date))
    //       output +=x.item.storage
          
    //       x['storage']=(x.item.storage/100).toFixed(2);
    //       if(x.item.source=='RECYCLER') x['storage'] = ((-x.item.storage/100).toFixed(2)); 
    //       if(x.item.paytype=='OFF') x['storage']= Math.abs((x.item.storage/100)).toFixed(2)
    //       x['item']['recyclename'] =  x['item']['recyclename'] ==null?'未知':x['item']['recyclename']
    //       x['sourcechinese'] = x.item.source == "BK_RECHARGE" ? '后台储值' :x.item.source=="RECYCLE"?'回收物品':x.item.source=="AGENT"?"代理商":x.item.source=="RECHARGE"?"充值":x.item.source=="RECYCLER"?"回收":x.item.source=="WITHDRAWAL"?"提现":"超管"
    //     })
    //     console.log(list.length,'list.length');
    //     that.setData({ output:(res.data.output1/ 100).toFixed(2) //(res.data.output/ 100).toFixed(2)
        
    //     })

    //     // if(that.data.usertype=="AGENT"){
    //     //   that.setData({ output: (res.data.output/ 100).toFixed(2)})

    //     // }
    //     list = oldlist.concat(list);

    //     let storage=that.data.usertype=='RECYCLER'?(res.data.output1/ 100).toFixed(2):parseFloat((res.data.storage1/100).toFixed(2)),totalprice=parseFloat((Withdrawalprice/100).toFixed(2))
    //     let a = res.data.withdrawal/100 - totalprice;
    //     console.log(a,'aaaaaaaaa');
        
    //     that.setData({list,storage,iswithdrawal, reachbtm: list.length == res.data.total,totalprice,canwithdrawal: !iswithdrawal&&(res.data.withdrawal/100 - totalprice>=0)})
    //   }
    // });
    // }else{ 
    //   api.getgrowthlist({pagesize,start},res => {
    //     wx.hideLoading({
    //       complete: (res) => {},
    //     })
    //     if (res.data.issuccess == 1) {
    //       all=res.data.growth
    //       res.data.list.forEach(x=>{
    //         x.updateDate = util.formatTimenosecend(new Date(x.updateDate))
    //         list.push(x)
    //       })
         
    //     }
    //     that.setData({all,list, reachbtm: list.length == res.data.total})
    //   });
    // }
  },
  cancelmodal(){
    this.setData({hiddenmodal:!0})
  },

  confirm(){
    console.log('confirm');
    let{inputmoney,withdrawal} = this.data,err=''
    inputmoney = Number(inputmoney)
    console.log(inputmoney,'inputmoney');
    if(isNaN(inputmoney)||inputmoney==0||inputmoney<1){
err='金额输入有误'
    }else  if(inputmoney>withdrawal||inputmoney>500){
      err="输入金额大于可提现金额"
    }
    if(err!=''){
      wx.showToast({
        title: err,
        icon:'none'
      })
    }else{
      console.log(inputmoney,'inputmoneyinputmoney');
       api.WithdrawalApplycreateapply(inputmoney*100,res=>{
      if(res.data.issuccess==1){
        if(res.data.iswithdrawal==1){
          // that.setData({iswithdrawaling:!1})
          that.setData({iswithdrawaling:!1,canwithdrawal:!1})
          wx.showModal({
            content:'提现成功',
            showCancel:!1,
          })
        }else if(res.data.iswithdrawal==2){
          that.setData({iswithdrawaling:!1,canwithdrawal:!0})
          wx.showModal({
            title: '提示',
            content:'每次提现金额最多提现五百元，超过五百元请多提现几次',
            showCancel:!1,
          })
         
        }else{
          that.setData({iswithdrawal:!0,iswithdrawaling:!1})
          wx.showModal({
            content:'申请成功',

            showCancel:!1,
          })
        }
      }else{
        that.setData({iswithdrawaling:!1})
        wx.showModal({
          content:res.data.msg,
          showCancel:!1,
        })
      }
    })


    }
   // console.log(err,'errerr');
  },

   // 提现
   withdrawal(){
     if(that.data.iswithdrawaling) return;
     that.setData({iswithdrawaling:!0})
    this.setData({hiddenmodal:!1})



    // api.WithdrawalApplycreateapply(res=>{
    //   if(res.data.issuccess==1){
    //     if(res.data.iswithdrawal==1){
    //       // that.setData({iswithdrawaling:!1})
    //       that.setData({iswithdrawaling:!1,canwithdrawal:!1})
    //       wx.showModal({
    //         content:'提现成功',
    //         showCancel:!1,
    //       })
    //     }else if(res.data.iswithdrawal==2){
    //       that.setData({iswithdrawaling:!1,canwithdrawal:!0})
    //       wx.showModal({
    //         title: '提示',
    //         content:'每次提现金额最多提现五百元，超过五百元请多提现几次',
    //         showCancel:!1,
    //       })
         
    //     }else{
    //       that.setData({iswithdrawal:!0,iswithdrawaling:!1})
    //       wx.showModal({
    //         content:'申请成功',

    //         showCancel:!1,
    //       })
    //     }
    //   }else{
    //     that.setData({iswithdrawaling:!1})
    //     wx.showModal({
    //       content:res.data.msg,
    //       showCancel:!1,
    //     })
    //   }
    // })



  },
  // formatData(list){
  //   let lists=[]
  //   list.forEach(x=>{
  //     x.updateDate = util.formatTimenosecend(new Date(x.updateDate))
  //     lists.push(x)
  //   })
  //   that.setData({lists,reachbtm:list.length})
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { reachbtm } = this.data;
    if (reachbtm) {
      return
    }
    that.setData({ start: that.data.start + that.data.pagesize }, () => {
      that.getdata()
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})